package com.chenjj.java.test.map.hashmap;

import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * 解读源码HashMap用到的一些测试用例
 */
public class HashMapTester {
    /**
     * 测试各种运算符
     */
    @Test
    public void test1(){
        System.out.println(1<<30);


        //hashMap tableSizeFor(cap)
        int MAXIMUM_CAPACITY = 1 << 30;
        int cap = 100;
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        System.out.println( (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1);


        //&& & 的区别
        System.out.println(20&15);
        System.out.println(true&&true);


        //测试&
        System.out.println("测试& start");
        for (int i = 0 ; i < 100 ; i++){
            System.out.println("15&"+i+"="+(15&i)+","+i+"%16="+(i%16));
        }
        System.out.println("测试& end");

        //DEBUG MAP
        HashMap<String,String> map = new HashMap<>(16);
        for (int i = 0; i < 1000; i++){
            map.put("a"+i,"a1");
        }
        HashMap<String,String> map1 = new HashMap<>(map);
        System.out.println(map.size());
        System.out.println(map1.size());

    }

    /**
     * 测试 HashMap 的ConcurrentModificationException
     * @throws InterruptedException
     */
    @Test
    public void test6() throws InterruptedException {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("GoddessY", 1);
        map.put("Joemsu1", 2);
        map.put("Joemsu", 3);

        Set<String > set = map.keySet();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            if ("Joemsu1".equalsIgnoreCase(key)) {
                map.remove(key); //异常
                //iterator.remove();;//正常
            }
        }

        System.out.println("123");
    }
}
